package rwt.server.handlers.web;

import rwt.server.*;
import rwt.server.RailroadMessages.LocoSndMsg;
import rwt.server.WebMessages.SetTrainNextSwitch;
import rwt.server.core.*;
import rwt.server.mtl.java.MessageTranslationLibrary.SwitchStateType;
import rwt.server.railroad.Train;

public class SetTrainNextSwitchHandler extends MessageHandler<SetTrainNextSwitch>
{
	@Override
	public void handle(SetTrainNextSwitch message, IConnection source) throws Exception
	{
		Train t = RailServer.getTrainManager().getTrainById(message.trainId);
		int slot = t.getVirtualSlotNum();
		
		if (message.closeOrThrow == SwitchStateType.Closed)
			t.setCloseNextSwitch(1 - t.getCloseNextSwitch());
		else if (message.closeOrThrow == SwitchStateType.Thrown)
			t.setThrowNextSwitch(1 - t.getThrowNextSwitch());
		LocoSndMsg locoSndMsg = new LocoSndMsg(slot, t.getCloseNextSwitch(), t.getThrowNextSwitch(), t.getMute());
		RailServer.getController().write(locoSndMsg);
	}
}
